<template>
  <PageWrapper>
    <a-button @click="getDevices" type="primary">获取数据</a-button>
    <a-button @click="geoTest" type="primary">加载geo</a-button>
    <div ref="wrapRef" :style="{ height, width }"></div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref, nextTick, unref, onMounted } from 'vue';
  import { useScript } from '@/hooks/web/useScript';

  import cnGeoJson from '@/assets/ns.json';
  import { PageWrapper } from '@/components/Page';

  defineOptions({ name: 'BaiduMap' });

  defineProps({
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: 'calc(100vh - 78px)',
    },
  });

  const wrapRef = ref<HTMLDivElement | null>(null);
  const { toPromise: toPromise1 } = useScript({
    src: 'https://api.map.baidu.com/getscript?v=3.0&ak=rU8qkC216C0zEXnKTeKYHzubOHFzowRL&services=&t=20210201100830&s=1',
  });
  const { toPromise: toPromise2 } = useScript({
    src: 'https://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js',
  });
  const { toPromise: toPromise3 } = useScript({
    src: 'https://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js',
  });
  const { toPromise: toPromise4 } = useScript({
    src: 'http://api.map.baidu.com/api?v=3.0&ak=rU8qkC216C0zEXnKTeKYHzubOHFzowRL&services=&t=20210201100830',
  });

  async function listAllDevice() {
    return fetch('http://localhost:8787/listAllLatLng').then((res) => {
      return res.json();
    });
  }

  let BMap;

  async function initMap() {
    await toPromise1();
    await toPromise2();
    await toPromise3();
    await toPromise4();
    await nextTick();
    const wrapEl = unref(wrapRef);
    if (!wrapEl) return;
    BMap = (window as any).BMap;
    const map = new BMap.Map(wrapEl);
    const point = new BMap.Point(114.0545429, 22.5445741);
    map.centerAndZoom(point, 1);
    map.enableScrollWheelZoom(true);
    // const marker = new BMap.Marker(point); // 创建标注
    // map.addOverlay(marker);
    return map;
  }

  function geoTest() {
    console.log(BMapGL.GeoJSONLayer);
    var geojsonLayer = new BMapGL.GeoJSONLayer({
      map: map,
      enableEditing: false, // 是否启用编辑模式，false表示禁用
      styleOptions: {
        strokeColor: 'blue', // 设置GeoJSON图层的边线颜色
        fillColor: 'yellow', // 设置GeoJSON图层的填充颜色
        strokeWeight: 2, // 设置GeoJSON图层的边线宽度
        fillOpacity: 0.5, // 设置GeoJSON图层的填充透明度
      },
    });
    geojsonLayer.addData(cnGeoJson);
    var legendControl = new window.BMapGL.Control({ anchor: window.BMAP_ANCHOR_TOP_LEFT });
    legendControl.addEventListener(
      'viewc[han](https://geek.csdn.net/educolumn/0d22b54eaf6bcf967d9625e1679d00b4?spm=1055.2569.3001.10083)ged',
      function () {
        // 图例控件viewc[han](https://geek.csdn.net/educolumn/0d22b54eaf6bcf967d9625e1679d00b4?spm=1055.2569.3001.10083)ged事件处理[函数](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)
      },
    );
    map.addControl(legendControl);
  }

  function getDevices() {
    listAllDevice().then((res) => {
      let set = new Set<string>();
      res.forEach((item) => {
        if (!set.has('' + item.lng + item.lat)) {
          set.add('' + item.lng + item.lat);
          const point = new BMap.Point(parseFloat(item.lng), parseFloat(item.lat));
          const marker = new BMap.Marker(point); // 创建标注
          map.addOverlay(marker);
        }
      });
    });
  }

  let map;

  onMounted(async () => {
    map = await initMap();
  });
</script>
